//package com.example.startapp;
//
//import androidx.appcompat.app.AppCompatActivity;
//
//import android.content.Intent;
//import android.content.SharedPreferences;
//import android.os.Bundle;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.ImageButton;
//import android.widget.Toast;
//
//import com.example.startapp.network.HttpConnect;
//import com.example.startapp.user.Kehu;
//
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import java.util.HashMap;
//import java.util.Map;
//
//public class YongHuActivity extends AppCompatActivity {
//
////    EditText userIdinput;
////    EditText userPwdinput;
////    Button userLogin;
////
////    @Override
////    protected void onCreate(Bundle savedInstanceState) {
////        super.onCreate(savedInstanceState);
////        setContentView(R.layout.activity_yong_hu);
////
////
////        userIdinput = findViewById(R.id.user_Id);
////        userPwdinput = findViewById(R.id.user_pwd);
////        userLogin = findViewById(R.id.login_yonghu);
////
////        // 登录按钮点击事件
////        userLogin.setOnClickListener(v -> loginUser());
////    }
////
////    private void loginUser() {
////        String userId = userIdinput.getText().toString();
////        String userPwd = userPwdinput.getText().toString();
////
////        // 创建 kehu 实例
////        Kehu loginRequest = new Kehu(userId, userPwd);
////
////        ApiService apiService = ApiClient.getClient().create(ApiService.class);
////        Call<User> call = apiService.loginUser(loginRequest); // 使用 LoginRequest 实例
////
////        call.enqueue(new Callback<User>() {
////            @Override
////            public void onResponse(Call<User> call, Response<User> response) {
////                Log.d("LoginActivity", "Response code: " + response.code());
////                Log.d("LoginActivity", "Response body: " + response.body());
////
////                if (response.isSuccessful() && response.body() != null) {
////                    // 登录成功，跳转到主页面
////                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
////                    startActivity(intent);
////                    finish();
////                } else {
////                    // 登录失败，提示用户
////                    Toast.makeText(LoginActivity.this, "用户账号或者密码错误", Toast.LENGTH_SHORT).show();
////                }
////            }
////
////            @Override
////            public void onFailure(Call<User> call, Throwable t) {
////                // 请求失败，提示用户
////                Toast.makeText(LoginActivity.this, "登录失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
////            }
////        });
////    }
////}
////
////
////
////
////    }
//private SharedPreferences loginPreference;
//    private EditText userId;
//    private EditText userPwd;
//    //private CheckBox remember;
//    private Button btn_login;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_yong_hu);
//         userId= (EditText) findViewById(R.id.user_Id);
//        userPwd = (EditText) findViewById(R.id.user_pwd);
//        //remember = (CheckBox) findViewById(R.id.remember);
//        btn_login = (Button) findViewById(R.id.login_yonghu);
//        loginPreference = getSharedPreferences("login", MODE_PRIVATE);
//
//        ///要通过loginPreference去记录三个参数（checked，userName，password）
//        boolean cheched = loginPreference.getBoolean("checked", false);
//        if (cheched) {
//            Map<String, Object> m = readLogin();
//            if (m != null) {
//                userId.setText((CharSequence) m.get("userName"));
//                userPwd.setText((CharSequence) m.get("password"));
//                //remember.setChecked(cheched);
//            }
//        }
//
//
//        btn_login.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //configLoginInfo(remember.isChecked());
//
//                login();
//            }
//        });
//    }
//
//    /**
//     * 配置登录信息
//     *
//     * @param checked
//     */
////    public void configLoginInfo(boolean checked) {
////        SharedPreferences.Editor editor = loginPreference.edit();
////        editor.putBoolean("checked", remember.isChecked());
////        if (checked) {
////            editor.putString("userName", userName.getText().toString());
////            editor.putString("password", password.getText().toString());
////        } else {
////            editor.remove("userName").remove("password");
////        }
////        editor.commit();
////    }
//
//    /**
//     * 读登录信息
//     * @return
//     */
//    public Map<String, Object> readLogin() {
//        Map<String, Object> m = new HashMap<>();
//        String userName = loginPreference.getString("userName", "");
//        String password = loginPreference.getString("password", "");
//        m.put("userName", userName);
//        m.put("password", password);
//        return m;
//    }
//
//    private void login() {
//        new Thread() {
//            @Override
//            public void run() {
//                String url = HttpConnect.BASE_URL + "/LoginServlet";
//                Map<String, String> params = new HashMap<String, String>();
//                String name = userId.getText().toString();
//                String p = userPwd.getText().toString();
//                params.put("userName", name);
//                params.put("password", p);
//                String result = HttpConnect.postHttpRequest(url, params);
//                System.out.println(result);
//                try {
//                    JSONObject json = new JSONObject(result);
//                    final Integer code = (Integer) json.get("code");
//                    System.out.println(code);
//                    YongHuActivity.this.runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (code == 1) {
//                                Intent intent = new Intent();
//                                //intent.setClass(MainActivity.this, FirstActivity.class);
//                                intent.setAction("test_1.test_1Activity");//跳转界面
//                                intent.addCategory(Intent.CATEGORY_DEFAULT);
//                                startActivity(intent);
//                                //Toast.makeText(MainActivity.this, "登录成功！",Toast.LENGTH_LONG).show();
//                            } else {
//                                Toast.makeText(YongHuActivity.this, "用户名或密码错误！",
//                                        Toast.LENGTH_LONG).show();
//                            }
//                        }
//                    });
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }.start();
//    }
//
//}